package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.WorkExperienceAdapter;
import com.wbao.dianniu.customView.MyListView;
import com.wbao.dianniu.data.JobDetailData;
import com.wbao.dianniu.data.WorkExperienceData;
import com.wbao.dianniu.listener.IJobDetailListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobDetailManager;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 简历预览
 * 
 * @author yejianjun
 *
 */
public class JobPreviewActivity extends BaseActivity implements IJobDetailListener {

	private LinearLayout waittingLayout;
	private ProgressBar waittingProBar;
	private TextView waittingTV;
	
	private 	ImageView iv_head;
	private 	TextView tv_name;
	private 	TextView tv_label;
	private 	TextView tv_post;
	private 	TextView tv_salary;
	private 	TextView tv_address;
	private 	TextView tv_experience;
	private 	TextView tv_education;
	private 	MyListView mListView;
	private JobDetailData data;
	private JobDetailManager manager;
	private boolean isModifyPreview = true;
	private Button rightBtn;
	private WorkExperienceAdapter adapter;
	private List<WorkExperienceData> experienceList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.job_preview,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.resume_preview));
		initWattingView();
		init();
		initListData();
		initData();
		if (!isModifyPreview) {
			manager = new JobDetailManager(this);
			manager.addJobDetailListener(this);
			manager.jobDetail(GlobalContext.accountId, GlobalContext.accountId);
		}else{
			waittingLayout.setVisibility(View.GONE);
			rightBtn.setVisibility(View.INVISIBLE);
		}
	}
	
	private void initData(){
		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra(GlobalContext.BUNDLE_DATA);
		if(null != bundle){
			PictureDownloadUtils.displayHeadImage(this,iv_head, bundle.getString(GlobalContext.BUNDLE_HEADPIC, ""));
			tv_name.setText(bundle.getString(GlobalContext.BUNDLE_REALNAME, ""));
			tv_label.setText(bundle.getString(GlobalContext.BUNDLE_LABEL, ""));
			tv_post.setText(bundle.getString(GlobalContext.BUNDLE_SUBJECT, ""));
			tv_salary.setText(bundle.getString(GlobalContext.BUNDLE_SALARY, ""));
			tv_address.setText(bundle.getString(GlobalContext.BUNDLE_ADDRESS, ""));
			tv_experience.setText(bundle.getString(GlobalContext.BUNDLE_EXPERIENCE, ""));
			tv_education.setText(bundle.getString(GlobalContext.BUNDLE_EDUCATION, ""));
			String describe = bundle.getString(GlobalContext.BUNDLE_CONTENT, "");
			if (!TextUtils.isEmpty(describe)) {
				experienceList = Utils.parseWorkData(describe);
				adapter.setData(experienceList);
				setListViewHeightBasedOnChildren(mListView);
				adapter.notifyDataSetChanged();
			}
		}else{
			isModifyPreview = false;
		}
	}
	
	private void init(){
		iv_head = (ImageView) findViewById(R.id.user_head);
		tv_name = (TextView) findViewById(R.id.user_name);
		tv_label = (TextView) findViewById(R.id.user_label);
		tv_post = (TextView) findViewById(R.id.job_preview_post);
		tv_salary = (TextView) findViewById(R.id.job_preview_money);
		tv_address = (TextView) findViewById(R.id.job_preview_place);
		tv_experience = (TextView) findViewById(R.id.job_preview_years);
		tv_education = (TextView) findViewById(R.id.job_preview_education);
		mListView = (MyListView) findViewById(R.id.job_preview_describle);
		rightBtn = getRightTextButton();
		rightBtn.setText(getResources().getString(R.string.share));
		rightBtn.setOnClickListener(this);
	}
	private void initListData(){
		experienceList = new ArrayList<WorkExperienceData>();
		adapter = new WorkExperienceAdapter(this,1);
		mListView.setAdapter(adapter);
		setListViewHeightBasedOnChildren(mListView);
	}
	private void initWattingView(){
		waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
		waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
		waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.right_button:
			// 调用分享内容
			String imageUrl;
			if (TextUtils.isEmpty(data.getHeadPic())) {
				imageUrl = GlobalContext.cdndownUrl + "logo.png";
			} else {
				imageUrl = GlobalContext.cdndownUrl + data.getHeadPic();
			}
			String content = "应聘岗位：" + data.getPost() + "\n" + "工作经验：" + data.getExperience();
			ShareSDKManager.getInstance().showShare(this, content, 4, imageUrl, data.getId());
			break;
		default:
			break;
		}
	}
	@Override
	public void onJobDetailFailure(int errorCode, String errorMsg) {
		waittingLayout.setVisibility(View.VISIBLE);
		waittingProBar.setVisibility(View.GONE);
		waittingTV.setText("加载失败，请稍后再试...");
	}
	@Override
	public void onJobDetailSuccess(JobDetailData data) {
		if(null == data){
			waittingLayout.setVisibility(View.VISIBLE);
			waittingProBar.setVisibility(View.GONE);
			waittingTV.setText("数据为空...");
			return;
		}
		waittingLayout.setVisibility(View.GONE);
		this.data = data;
		display(data);
	}
	private void display(JobDetailData data){
		if(!TextUtils.isEmpty(data.getHeadPic())){
			PictureDownloadUtils.displayHeadImage(this,iv_head, data.getHeadPic());
		}
		tv_name.setText(data.getRealName() == null ? "": data.getRealName());
		tv_label.setText(data.getLabel() == null ? "": data.getLabel());
		tv_post.setText(data.getPost() == null ? "": data.getPost());
		tv_salary.setText(data.getSalary() == null ? "": data.getSalary());
		tv_address.setText(data.getAddress() == null ? "": data.getAddress());
		tv_experience.setText(data.getExperience() == null ? "": data.getExperience());
		tv_education.setText(data.getEducation() == null ? "": data.getEducation());
		if (!TextUtils.isEmpty(data.getDescribe())) {
			experienceList = Utils.parseWorkData(data.getDescribe());
			adapter.setData(experienceList);
			setListViewHeightBasedOnChildren(mListView);
			adapter.notifyDataSetChanged();
		}
	}
	
	public void setListViewHeightBasedOnChildren(ListView listView) {   
        // 获取ListView对应的Adapter   
        int totalHeight = 0;   
        for (int i = 0, len = adapter.getCount(); i < len; i++) {   
            // listAdapter.getCount()返回数据项的数目   
            View listItem = adapter.getView(i, null, listView);   
            // 计算子项View 的宽高   
            listItem.measure(0, 0);    
            // 统计所有子项的总高度   
            totalHeight += listItem.getMeasuredHeight();    
        }   
   
        ViewGroup.LayoutParams params = listView.getLayoutParams();   
        params.height = totalHeight+ (listView.getDividerHeight() * (adapter.getCount() - 1));   
        // listView.getDividerHeight()获取子项间分隔符占用的高度   
        // params.height最后得到整个ListView完整显示需要的高度   
        listView.setLayoutParams(params);   
    }   
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
